package cn.com.yusys.yusp.message.service.message.mq.rabbitmq;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;

import cn.com.yusys.yusp.commons.util.DateUtil;
import cn.com.yusys.yusp.message.domain.MessagePool;
import cn.com.yusys.yusp.message.service.message.MessageSendThread;
import cn.com.yusys.yusp.message.service.message.mq.MessageQueueConstants;

@Component
public class RabbitMQMessageReceiver {
	@Autowired
	private MessageSendThread messageSendThread;
	
	private ObjectMapper mapper = new ObjectMapper();
    
	private final Logger log = LoggerFactory.getLogger(RabbitMQMessageReceiver.class);
	
	@RabbitListener(queues = MessageQueueConstants.MESSAGE_QUEUE)
    public void queue(Message message, Channel channel){
		try {
			System.out.println(DateUtil.formatDate(DateUtil.PATTERN_DATETIME_COMPACT) + ": Receiver : " + new String(message.getBody()) + " >> "
					+ message.getMessageProperties().getDeliveryTag());
			
			String msgStr = new String(message.getBody());			
			log.debug("Receive real-time messages : " + msgStr );			
			MessagePool messagePool= mapper.readValue(msgStr,MessagePool.class);
			messageSendThread.sendMessageThread(messagePool);
			
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
		}catch (Exception e) {
			try {
				channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
			} catch (IOException e1) {
				
			}
		}
    }
	
	@RabbitListener(queues = MessageQueueConstants.MESSAGE_DELAY_QUEUE)
	public void delayQueue(Message message, Channel channel) {
		try {
		    String hmPattern = "HH:mm";
			long now = DateUtil.parseDate(DateUtil.formatDate(hmPattern), hmPattern).getTime();
			long start = DateUtil.parseDate(MessageQueueConstants.START_TIME, hmPattern).getTime();
			long end = DateUtil.parseDate(MessageQueueConstants.END_TIME, hmPattern).getTime();

			if (now < start || now > end) {
				if (now < start) {
					try {
						long time = start - now;
						Thread.sleep(time);
					} catch (InterruptedException e) {
					    // remarks by Cytus_ at 20180904 修改sonar扫描出现的bug， 此处忽略
						e.printStackTrace();
					}
				} else {
					try {
						Thread.sleep(now - end);
					} catch (InterruptedException e) {
						// remarks by Cytus_ at 20180904 修改sonar扫描出现的bug， 此处忽略
						e.printStackTrace();
					}
				}
			}

			String msgStr = new String(message.getBody());			
			log.debug("Receive timing messages : " + msgStr );		
			MessagePool messagePool= mapper.readValue(msgStr,MessagePool.class);
			messageSendThread.sendMessageThread(messagePool);
			
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
		}catch (Exception e) {
			e.printStackTrace();
			try {
				channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
			} catch (IOException e1) {
				
			}
		}

	}
}
